package com.finalcial.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.finalcial.po.LoginMsg;
import com.finalcial.po.OtherInformation;
import com.finalcial.service.ApplicationService;

@Controller
@RequestMapping("/travelApplication")//访问国内差旅申请表
public class TravelApplicationController {
	
	@Resource
	private ApplicationService applicationService;

	@RequestMapping("/travelBasic")
	public ModelAndView listTravelApplicationMessage(HttpSession httpSession) {
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId =  loginMsg.getUserId();
		//显示所有可选择的接收者角色
		List<Map<String, Object>> roleNameList = applicationService.showRoleNameList();
		//显示申请人基本信息
		List<Map<String,Object>> invoiceTypeList = applicationService.showInvoiceTypeName();//票据类型信息
		List<Map<String, Object>> basicList = applicationService.showBasicList(userId);//申请人基本信息
		//显示内部人信息
		OtherInformation otherMessage = applicationService.showInsiderInfoOther(userId);
		ModelAndView mav = new ModelAndView();
		mav.addObject("roleNameList", roleNameList);
		mav.addObject("invoiceTypeList", invoiceTypeList);
		mav.addObject("basicList", basicList);
		mav.addObject("otherMessage", otherMessage);
		mav.setViewName("expenseplatform/apply/domestictravelApply");//访问国内差旅申请表
		return mav;
		
	}
	
	//点击角色--显示用户下拉框信息
	@ResponseBody 
	@RequestMapping(value="/listUserNameAll", method = RequestMethod.POST)
	public List<Map<String, Object>> listUserNameAll(HttpServletRequest request, HttpServletResponse response) {
		String roleId = request.getParameter("roleId");
		List<Map<String, Object>> userNameList = applicationService.listUserNameAll(Integer.valueOf(roleId));
		for(Map<String, Object> map:userNameList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		return userNameList;
	}
	
	
	//国内差旅界面--新增国内差旅信息
		@ResponseBody
		@RequestMapping(value="/insertTravelMsg", method = RequestMethod.POST)
		public Integer insertTravelApplication(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
			
			//(小写的mm表示的是分钟 )转换时间 
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			
			//用户ID
			LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
			Integer userId = loginMsg.getUserId();	
			//收支方式
			String payments = request.getParameter("payments");
			//报销ID
			String reimbursementId = request.getParameter("reimbursementId");
			//报销时间
			Date reimbursementTime = null;
			//报销说明
			String reimbursementExplain = request.getParameter("reimbursementExplain");
			//附单据张数
			Integer reimbursementNumber = Integer.parseInt(request.getParameter("reimbursementNumber"));
			//报账金额（含税）
			Integer reimbursementMoney = Integer.parseInt(request.getParameter("reimbursementMoney"));
			//报账金额（不含税）
			Integer reimbursementMoneyOfTax = Integer.parseInt(request.getParameter("reimbursementMoneyOfTax"));
			//接收者id
			Integer accepterId = Integer.parseInt(request.getParameter("accepterId"));
			try {
					reimbursementTime  =sdf.parse(request.getParameter("reimbursementTime"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
				
			System.out.println(userId+" "+payments+" "+reimbursementId+" "+reimbursementTime+" "+reimbursementExplain+" "+reimbursementNumber+" "+reimbursementMoney+" "+reimbursementMoneyOfTax+" "+" "+accepterId);
			Integer state = applicationService.insertTravelApplicationMsg(userId, payments, reimbursementId, reimbursementTime, reimbursementExplain, reimbursementNumber, reimbursementMoney, reimbursementMoneyOfTax, accepterId);	
			return state;	
		}	
	
		/*根据用户ID得到费用类型金额*/
		@ResponseBody 
		@RequestMapping(value="/getFoodSalary", method = RequestMethod.POST)
		public List<Map<String, Object>> getFoodSalary(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
			LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
			Integer userId = loginMsg.getUserId();	
			List<Map<String, Object>> costTypeSalaryList = applicationService.getFoodSalary(userId);
			System.out.println(costTypeSalaryList);
			return costTypeSalaryList;
		}
		
		//国内差旅界面--新增国内差旅的差旅明细
		@ResponseBody
		@RequestMapping(value="/insertTravelDetailsMsg", method = RequestMethod.POST)
		public Integer insertTravelDetailsApplication(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
			
			//(小写的mm表示的是分钟 )转换时间 
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			
			//用户ID
			LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
			Integer userId = loginMsg.getUserId();	
			//报销ID
			String reimbursementId = request.getParameter("reimbursementId");
			//出差开始日期
			Date businessTravelStartDate = null;
			//出差结束日期
			Date businessTravelEndDate = null;
			//出发地
			String businessTravelStartSection = request.getParameter("businessTravelStartSection");
			//目的地
			String businessTravelEndSection = request.getParameter("businessTravelEndSection");
			//交通工具
			String vehicleName = request.getParameter("vehicleName");
			//伙食补贴费
			Integer foodCost = Integer.parseInt(request.getParameter("foodCost"));
			//城市间交通费
			Integer cityTrafficExpense = Integer.parseInt(request.getParameter("cityTrafficExpense"));
			//市内交通费
			Integer provinceTrafficExpense = Integer.parseInt(request.getParameter("provinceTrafficExpense"));
			//住宿费
			Integer hotelExpense = Integer.parseInt(request.getParameter("hotelExpense"));
			//进项税
			Integer inputTax = Integer.parseInt(request.getParameter("inputTax"));
			//其他费用
			Integer otherCost = Integer.parseInt(request.getParameter("otherCost"));
			try {
				businessTravelStartDate  =sdf.parse(request.getParameter("businessTravelStartDate"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			try {
				businessTravelEndDate  =sdf.parse(request.getParameter("businessTravelEndDate"));
			} catch (ParseException e) {
				e.printStackTrace();
			}				
			System.out.println(userId+" "+reimbursementId+" "+businessTravelStartDate+" "+businessTravelEndDate+" "+businessTravelStartSection+" "+businessTravelEndSection+" "+vehicleName+" "+foodCost+" "+cityTrafficExpense+" "+provinceTrafficExpense+" "+hotelExpense+" "+inputTax+" "+otherCost);
			Integer state = applicationService.insertTravelDetailsApplication(userId, reimbursementId, businessTravelStartDate, businessTravelEndDate, businessTravelStartSection, businessTravelEndSection, vehicleName, foodCost, cityTrafficExpense, provinceTrafficExpense, hotelExpense, inputTax, otherCost);	
			return state;	
		}	
}
